/*
*******************************************************************************
*                    Copyright (c) 2013, TQ System
*                             All Rights Reserved
*
* Purpose           : COM file
* File Name         : PUBLIC.H

* Revision history  :
    01a,2018/05/16,Ryan  -- Creation(written)

* Description       :

*******************************************************************************
*/
#include <pthread.h>

#ifndef _AP_PUBLIC_INCLUDED_
#define _AP_PUBLIC_INCLUDED_

#define IO_DI_MAXLEN 16
#define IO_DO_MAXLEN 8
#define IO_DO_OID 0x0002
#define IO_DI_OID 0x0001

#define MAX_BUFF_LEN 409600
#define MQ_SENT_BUFF_LEN 2048
#define RECV_BUFF_LEN 2048
#define MAX_MQSEND_PK 200
#define MAX_PACKESUM_LEN 200
//#define BUSY 1;
//#define IDLE 0;

typedef unsigned char BOOLEAN;
typedef unsigned char INT8U;
typedef signed char INT8S;
typedef unsigned short INT16U;
typedef signed short INT16S;
typedef unsigned int INT32U;
typedef signed int INT32S;

#define MQTPA2DTU 0X0201
#define MQTPB2DTU 0X0301
#define MQTPC2DTU 0X0401

#define DTU2MQTPA 0X0102
#define DTU2MQTPB 0X0103
#define DTU2MQTPC 0X0104

#define SET_0 0

#define READ_10 10
#define READ_11 11
#define READ_30 11
#define READ_100 100

#define ALM_20 20
#define SET_1 1


#define mCmd_getData        0x01      
#define mCmd_setData        0x02       
#define mCmd_report         0x03  
#define mCmd_retransmission 0x04  
#define mCmd_getDevRealData 0x80  
#define mCmd_getSqilte      0x81  
#define mCmd_setSqilte      0x82 
#define mCmd_kaizhanReport  0x83 
#define mCmd_devReset       0x84 
// #define mCmd_pollAllRealDevData     0x85

#define MCU_UART_IDX         0
#define BLE_UART_IDX         1

#define mRsp_success        0x00        

#define mRsp_condition      0x01       
#define mRsp_cmdError       0x02   

#define PACKED __attribute__((aligned(1),packed))
typedef struct{
	unsigned char        startFlag;
	unsigned short int   packetLen;
	unsigned short int   packetNum;
	unsigned long  int   siteId;
	unsigned char        devId;
	unsigned char        packetCmd;
	unsigned char        packetAnswer;

}PACKED SERVER_COM_HEAD_TYPE;
#define SERVER_COM_PACKET_HEAD_LEN  (sizeof(SERVER_COM_HEAD_TYPE))
// typedef union
// {
	
//     SERVER_COM_HEAD_TYPE   head;
// 	unsigned char          *buffer;
// } SERVER_COM_TYPE;

typedef struct
{
	volatile unsigned long int readPos;
	volatile unsigned long int writePos;
	unsigned char data[MAX_BUFF_LEN];
	volatile int packetSum;
	pthread_mutex_t lock;
	pthread_cond_t newPacketFlag;

} DATAS_BUFF_T;

typedef struct
{
	unsigned char data[RECV_BUFF_LEN];
	unsigned int protoltype;
	unsigned int scrFlag; //data from where
	unsigned int len;
	pthread_mutex_t lock;
	pthread_cond_t newPacketFlag;
	volatile int packetSum;
} RECV_BUFF_T;
typedef struct
{
	unsigned char data[MAX_MQSEND_PK][MQ_SENT_BUFF_LEN];
	unsigned int mqttTopicFlag[MAX_MQSEND_PK];
	unsigned int len[MAX_MQSEND_PK];
	volatile int packetSum;
	volatile int rdPkIndx;
	volatile int wrPkIndx;
	pthread_mutex_t lock;
} MQTT_SENT_BUFF_T;
typedef struct
{
	unsigned char data[MQ_SENT_BUFF_LEN];
	unsigned int len;
	unsigned int idx;
	unsigned int topicFlag;
} MQTT_TEMP;
typedef struct
{
	int oid;
	int oidLen;
	int devValInt;
	int sqlValue;
	int serValInt;
	int valueIntPre;
	int radio;
	int sensorId;
	int paraType;
	int enb;
	int updateAt;
	int longDataAtListAddr;
	char oidName[50];
	char valueString[30];

} jxDevDataTab;
enum  {IDLE=0, WORKING,WAITING};
typedef struct
{
	INT16U id;
	INT16U oid_actDevice;
    INT16U oid_triggerDevice;
	INT16U linkageType;
	INT32U linkageLoopPeriod;
	INT32U linkageStartTime;
	INT32U linkageKeepTime;
	INT32U value;
	INT32U actBitMask;
	INT32U curTime;
	INT32U startActionTime;
	INT8U  status;
} JXLINKAGETAB;

typedef struct
{
	int oid;
	int valueInt;
	int valueIntPre;
	int radio;
	int upSentPeriod;
	int belongToOid;
	int dataOption;
	int belongOidIdx;
	char ssDevId[5];
	char valueString[10];
	char ssType[30];
	char ssDataType[30];
	char ssTypeCnName[50];
	char oidName[50];

} devDataTable;

typedef struct
{
	char name[10];
	int  speed;
	int  databits;
	int  stopbits;
	int  parity;
	char option;
} RS485CFG;
typedef struct
{
	INT8U  option;
	INT8U  value;
	INT32U oid;
} INPUTCfg;
typedef struct
{
	INT8U  option;
	INT8U  value;
	INT32U oid;
} OUTPUTCfg;
typedef struct
{
	INT32U oid;
	INT16U len;
	INT16U mbDevAddr;
	INT8U stdMbReadCmd;
	INT8U stdMbSetCmd;
	INT16U mbStartAddr;
	INT8U pollPortNameIdx; 
	INT16U comPacketIndex;
	INT16U byteIndex;
	INT16U bitIndex;
	INT16U nonStdCmdId;
	INT16U option;
	INT8U devValue[4];
	INT8U serValue[4];
	INT8U  rwFlag;
	INT16U multiValue;
	INT16U devideValue;
	INT16S addSubValue;
	INT8U  adjustFlag;
	INT32U mbLongDataAtListStartAddr;
	//INT8U  test[3];
}MBPOLLCFG;

enum MBSETSTATUS {SET_IDLE=0, SET_SENDING,SET_RECVING};

typedef struct
{
	INT8U  sendLen;
	INT8U  recvLen;
	INT8U  ctx[30];
	INT8U  status;
	INT8U  cmd;
	INT8U  setErrCnt;
	INT32U oid;

}MBSETLIST;

typedef struct
{
	unsigned int fileLen;
	unsigned int pkMaxLen;
	unsigned int pkSum;
	unsigned int pkCrc;
	unsigned int curPkIndx;
	unsigned int curState;
	char dlName[50];
} DOWNLOAD;

typedef struct
{

	INT8U name[30];
	INT8U content[50];
} DevCfgTable;

typedef union {
	INT8U byte[2];
	INT16U word;
} Word4Byte;
typedef union {
	INT8U byte[4];
	INT32U lword;
} lWord4Byte;
typedef struct
{
	INT8U devAddr;
	INT8U cmd;
	INT16U starMbAddr;
	INT16U byteSum;
	INT16U crc;
} CONTENT_RECORD;

typedef struct
{
	INT16U packetInex;
	INT16U starAddrIndex; //在总表中的开始位置。方便寻址
	INT8U portIdx;
	INT16U spCmdId;
	INT16U recLen;
	INT16U sendLen;
	CONTENT_RECORD content;
} PACKET_record;

typedef struct
{
	INT16U id;
	INT16U sendLen;
	INT16U recLen;
	INT16U paraGetStartAddr;
	INT8U ctx[20];
} nonStdMbCmdTab;
typedef struct
{
	INT16U sendLen;
	INT16U recLen;
	INT16U gCommIdx;
	INT8U ctx[20];
} MBSEND_T;
typedef struct {
	INT8U            devVal[4]; 
	INT8U            serVal[4]; 
	INT8U            setFlag; 
}MCU_MONITOROBJ_VAL;

typedef struct {
	INT8U     ol;        
	INT32U    oid;        
	INT8U 	  rwFlag;
	INT8U 	  opt2;
}MCU_MONITOROBJ_TAB;
typedef struct {
     
	INT32U    oid;        
	INT8U 	  writeFlag;
	INT8U 	  value;
}LINKAGE_CONTROL;

extern char get_jxOidValue(INT32U oid,INT8U *value);
extern char set_jxOidValue(INT32U oid,INT8U *value);
extern char set_mbOidDevValue(INT32U oid, INT8U *value);
extern int get_jxOidParaLen(INT32U oid);
extern int get_jxOidIdx(INT32U oid);
extern int get_typeValue(char *ssType);
extern void testCrc32(void);
extern INT16U modbusCrc16(INT8U *buffer, INT16U buffer_length);
extern INT32U caculate_crc(unsigned char *string, INT32U size);
extern int base64_decode(const char *base64, unsigned char *bindata);
extern char *base64_encode(const unsigned char *bindata, char *base64, int binlength);
extern void DebugPrint(char *fmt, ...);
extern void StrToHex(INT8U *pbDest, INT8U *pbSrc, INT16U nLen);
extern void rmZfFun(INT8U *p, INT8U ch);
extern void HexToStr(INT8U *pbDest, INT8U *pbSrc, INT16U nLen);
extern INT16U nativeUartReadTreat(char *recvBuf, INT16U recLen);
extern INT16U nativeUartSetTreat(void);
extern void modbusRecDataTreat(INT16U CommIdx, INT16U recLen,char *recBuf);
extern void modbusSetResposeDataTreat(INT32U oid, INT8U cmd,INT8U recLen, char *recBuf);
extern void  getTimeString(char *temp);
extern int  sql_general_interface(char *strSql,char *recvbuf);
// extern int  sql_general_set_interface(char *strSql,char *recvbuf);
extern int  sql_general_set_interface(char *strSql);
extern int get_jxDevAllRealData(INT8U *buff);
extern char set_jxDevDataValue(INT8U *buff,int len);
//extern void calc_img_crc(void);
extern INPUTCfg g_IOinput[];
extern OUTPUTCfg g_IOoutput[];
extern jxDevDataTab *g_jxDevDataTab;
extern devDataTable *g_devDataTab;
extern RS485CFG *g_RS485Cfg;
extern MBPOLLCFG *g_mbPollTab;
extern JXLINKAGETAB *g_jxLinkageTab;
extern char g_mbLongDataList[];
extern char g_jxDevLongDataList[];
extern PACKET_record *g_CommPacket;
extern nonStdMbCmdTab *g_nonStdMbCmdPacket;
extern INT16U g_tabLen;
extern INT16U g_jxTabLen;
extern INT16U g_jxLinkageTabLen;
extern INT16U g_RS485CfgLen;
extern INT16U g_mbPollTabLen;
extern INT16U g_nonStdMbCmdTabLen;
extern INT16U g_comPackeIdx;
extern char *gs_siteId;

extern char *g_mqComVer;
extern char g_mqComId[];
extern LINKAGE_CONTROL g_linkageControl;
extern char g_mqTopicReport[];
extern char g_mqTopicCtrl[];
extern char g_mqServer[];
extern char g_mqClientId[];
extern RECV_BUFF_T RecvBuff4treat;
extern MQTT_SENT_BUFF_T mqSentBuff;
extern DATAS_BUFF_T comBuff0;
extern char g_nativeUartRcvBuf[];
extern char g_nativeUartSendBuf[];
extern MCU_MONITOROBJ_VAL mcuMonitorObjVal[];
extern MBSETLIST g_mbSetPakcetList[];
#endif
